package com.kateba.android.ime;

import android.content.Intent;
import android.graphics.Color;
import android.inputmethodservice.InputMethodService;
import android.inputmethodservice.KeyboardView.OnKeyboardActionListener;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.CompletionInfo;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.kateba.android.blueinput.GUIBlueinput;
import com.kateba.android.blueinput.Singleton;
import com.kateba.android.hid.KBLayout;


public class VirtualKeyboard extends InputMethodService
  implements OnKeyboardActionListener
{
  private static final String LOG_TAG = "BlueInput::VIRTUALKB";
  static final boolean PROCESS_HARD_KEYS = true;
  static final int idSpinLayouts = 26;
  private boolean mCapsLock;
  final Handler mHandler = new Handler();
  private long mLastShiftTime;
  protected LinearLayout m_Panel;
  protected TextView m_bEdit;
  protected ImageView m_bHide;
  protected ImageView m_bLayout;
  protected ImageView m_bLayout2;
  protected ImageView m_bOptions;
  public boolean m_bVKReady = false;
  protected ListView m_lvSearch;
  String[] szLayouts;

  private void checkToggleCapsLock()
  {
    long l = System.currentTimeMillis();
    boolean bool;
    if (800L + this.mLastShiftTime > l)
      if (this.mCapsLock)
      {
        bool = false;
        this.mCapsLock = bool;
      }
    for (this.mLastShiftTime = 0L; ; this.mLastShiftTime = l)
    {
      //return;
      bool = true;
      break;
    }
  }

  private void handleBackspace()
  {
    Singleton.getInstance().Debug("BlueInput::VIRTUALKB", "handleBackspace() called.");
    keyDownUp(67);
    updateShiftKeyState(getCurrentInputEditorInfo());
  }

  private void handleCharacter(int paramInt, int[] paramArrayOfInt)
  {
    Singleton.getInstance().Debug("BlueInput::VIRTUALKB", "handleCharacter() called.");
    if (isInputViewShown())
      paramInt = Character.toUpperCase(paramInt);
    getCurrentInputConnection().commitText(String.valueOf((char)paramInt), 1);
  }

  private void handleClose()
  {
    Singleton.getInstance().Debug("BlueInput::VIRTUALKB", "handleClose() called.");
    requestHideSelf(0);
  }

  private void handleShift()
  {
    Singleton.getInstance().Debug("BlueInput::VIRTUALKB", "handleShift() called.");
    checkToggleCapsLock();
  }

  private void keyDownUp(int paramInt)
  {
    Singleton.getInstance().Debug("BlueInput::VIRTUALKB", "keyDownUp() called.");
    getCurrentInputConnection().sendKeyEvent(new KeyEvent(0, paramInt));
    getCurrentInputConnection().sendKeyEvent(new KeyEvent(1, paramInt));
  }

  public void onCreate()
  {
    super.onCreate();
    this.m_bVKReady = true;
    Singleton.getInstance().Debug("BlueInput::VIRTUALKB", "onCreate() called ");
    Singleton.getInstance().m_vkInst = this;
  }

  public View onCreateCandidatesView()
  {
    Singleton.getInstance().Debug("BlueInput::VIRTUALKB", "onCreateCandidatesView() called.");
    return null;
  }

  public View onCreateInputView()
  {
    int i = (int)(10.0F * Singleton.getInstance().getSCRDensity());
    if (i == 0)
      i = 10;
    this.m_bVKReady = true;
    Singleton.getInstance().Debug("BlueInput::VIRTUALKB", "onCreateInputView() called:" + i);
    this.m_bOptions = new ImageView(this.m_Panel.getContext());
    this.m_bOptions.setImageDrawable(getResources().getDrawable(2130837528));
    this.m_bOptions.setLayoutParams(new LinearLayout.LayoutParams(-2, -2));
    this.m_bOptions.setPadding(0, 0, i, 0);
    this.m_bOptions.setOnClickListener(new View.OnClickListener()
    {
      public void onClick(View paramView)
      {
        Intent localIntent = new Intent(VirtualKeyboard.this, GUIBlueinput.class);
        localIntent.addFlags(268435456);
        VirtualKeyboard.this.startActivity(localIntent);
      }
    });
    this.m_Panel.addView(this.m_bOptions);
    String[] arrayOfString = new String[10];
    arrayOfString[0] = getResources().getString(2131034137);
    arrayOfString[1] = getResources().getString(2131034138);
    arrayOfString[2] = getResources().getString(2131034139);
    arrayOfString[3] = getResources().getString(2131034140);
    arrayOfString[4] = getResources().getString(2131034141);
    arrayOfString[5] = getResources().getString(2131034142);
    arrayOfString[6] = getResources().getString(2131034143);
    arrayOfString[7] = getResources().getString(2131034144);
    arrayOfString[8] = getResources().getString(2131034145);
    arrayOfString[9] = getResources().getString(2131034146);
    this.szLayouts = arrayOfString;
    this.m_bLayout = new ImageView(this.m_Panel.getContext());
    this.m_bLayout.setImageDrawable(getResources().getDrawable(2130837532));
    this.m_bLayout.setLayoutParams(new LinearLayout.LayoutParams(-2, -2));
    this.m_bLayout.setPadding(0, 0, i, 0);
    this.m_bLayout.setOnClickListener(new View.OnClickListener()
    {
      public void onClick(View paramView)
      {
        int i = (1 + Singleton.getInstance().m_sett.getLayoutID()) % 10;
        Singleton.getInstance().m_sett.setLayoutID(i);
        VirtualKeyboard.this.m_bEdit.setText(VirtualKeyboard.this.szLayouts[i]);
        KBLayout.LoadLayout(VirtualKeyboard.this, i);
        Toast.makeText(VirtualKeyboard.this.getBaseContext(), VirtualKeyboard.this.getResources().getString(2131034174) + " " + VirtualKeyboard.this.szLayouts[i], 0).show();
      }
    });
    this.m_Panel.addView(this.m_bLayout);
    this.m_bEdit = new TextView(this.m_Panel.getContext());
    this.m_bEdit.setText(this.szLayouts[Singleton.getInstance().m_sett.getLayoutID()]);
    this.m_bEdit.setLayoutParams(new LinearLayout.LayoutParams(-2, -2));
    this.m_bEdit.setEnabled(false);
    this.m_bEdit.setPadding(0, 0, i, 0);
    this.m_bEdit.setTextColor(Color.rgb(200, 200, 200));
    this.m_Panel.addView(this.m_bEdit);
    this.m_bLayout2 = new ImageView(this.m_Panel.getContext());
    this.m_bLayout2.setImageDrawable(getResources().getDrawable(2130837533));
    this.m_bLayout2.setLayoutParams(new LinearLayout.LayoutParams(-2, -2));
    this.m_bLayout2.setPadding(0, 0, i, 0);
    this.m_bLayout2.setOnClickListener(new View.OnClickListener()
    {
      public void onClick(View paramView)
      {
        int i = -1 + Singleton.getInstance().m_sett.getLayoutID();
        if (i < 0)
          i = 9;
        Singleton.getInstance().m_sett.setLayoutID(i);
        VirtualKeyboard.this.m_bEdit.setText(VirtualKeyboard.this.szLayouts[i]);
        KBLayout.LoadLayout(VirtualKeyboard.this, i);
        Toast.makeText(VirtualKeyboard.this.getBaseContext(), VirtualKeyboard.this.getResources().getString(2131034174) + " " + VirtualKeyboard.this.szLayouts[i], 0).show();
      }
    });
    this.m_Panel.addView(this.m_bLayout2);
    this.m_bHide = new ImageView(this.m_Panel.getContext());
    this.m_bHide.setImageDrawable(getResources().getDrawable(2130837514));
    this.m_bHide.setLayoutParams(new LinearLayout.LayoutParams(-2, -2));
    this.m_bHide.setPadding(0, 0, 0, 0);
    this.m_bHide.setOnClickListener(new View.OnClickListener()
    {
      public void onClick(View paramView)
      {
        VirtualKeyboard.this.m_Panel.setVisibility(8);
        Toast.makeText(VirtualKeyboard.this.getBaseContext(), VirtualKeyboard.this.getResources().getString(2131034171), 1).show();
      }
    });
    this.m_Panel.addView(this.m_bHide);
    this.m_Panel.setKeepScreenOn(true);
    return this.m_Panel;
  }

  public void onDisplayCompletions(CompletionInfo[] paramArrayOfCompletionInfo)
  {
    Singleton.getInstance().Debug("BlueInput::VIRTUALKB", "onDisplayCompletions() called.");
  }

  public void onFinishInput()
  {
    super.onFinishInput();
    Singleton.getInstance().Debug("BlueInput::VIRTUALKB", "onFinishInput() called.");
  }

  public void onInitializeInterface()
  {
    if ((int)(10.0F * Singleton.getInstance().getSCRDensity()) == 0);
    Singleton.getInstance().Debug("BlueInput::VIRTUALKB", "onInitializeInterface() called.");
    this.m_Panel = new LinearLayout(getBaseContext());
    this.m_Panel.setLayoutParams(new LinearLayout.LayoutParams(-2, -2));
    this.m_Panel.setOrientation(0);
    this.m_Panel.setBackgroundColor(Color.argb(190, 0, 0, 0));
    this.m_Panel.setPadding(0, 0, 0, 0);
    this.m_Panel.setGravity(17);
  }

  public void onKey(int paramInt, int[] paramArrayOfInt)
  {
    Singleton.getInstance().Debug("BlueInput::VIRTUALKB", "onKey() called.");
    updateShiftKeyState(getCurrentInputEditorInfo());
    if (paramInt == -5)
      handleBackspace();
    while (true)
    {
    //  return;
      if (paramInt == -1)
      {
        handleShift();
        continue;
      }
      if (paramInt == -3)
      {
        handleClose();
        continue;
      }
      handleCharacter(paramInt, paramArrayOfInt);
    }
  }

  public boolean onKeyDown(int paramInt, KeyEvent paramKeyEvent)
  {
    Singleton.getInstance().Debug("BlueInput::VIRTUALKB", "onKeyDown() called:" + paramInt);
    return super.onKeyDown(paramInt, paramKeyEvent);
  }

  public boolean onKeyUp(int paramInt, KeyEvent paramKeyEvent)
  {
    Singleton.getInstance().Debug("BlueInput::VIRTUALKB", "onKeyUp() called." + paramInt);
    return super.onKeyUp(paramInt, paramKeyEvent);
  }

  public void onPress(int paramInt)
  {
    Singleton.getInstance().Debug("BlueInput::VIRTUALKB", "onPress() called.");
  }

  public void onRelease(int paramInt)
  {
    Singleton.getInstance().Debug("BlueInput::VIRTUALKB", "onRelease() called.");
  }

  public void onStartInput(EditorInfo paramEditorInfo, boolean paramBoolean)
  {
    super.onStartInput(paramEditorInfo, paramBoolean);
    Singleton.getInstance().Debug("BlueInput::VIRTUALKB", "onStartInput() called.");
    updateShiftKeyState(paramEditorInfo);
  }

  public void onStartInputView(EditorInfo paramEditorInfo, boolean paramBoolean)
  {
    super.onStartInputView(paramEditorInfo, paramBoolean);
    Singleton.getInstance().Debug("BlueInput::VIRTUALKB", "onStartInputView() called.");
    this.m_bEdit.setText(this.szLayouts[Singleton.getInstance().m_sett.getLayoutID()]);
  }

  public void onText(CharSequence paramCharSequence)
  {
    Singleton.getInstance().Debug("BlueInput::VIRTUALKB", "onText() called.");
    InputConnection localInputConnection = getCurrentInputConnection();
    if (localInputConnection == null);
    while (true)
    {
//      return;
      localInputConnection.beginBatchEdit();
      localInputConnection.commitText(paramCharSequence, 0);
      localInputConnection.endBatchEdit();
      updateShiftKeyState(getCurrentInputEditorInfo());
    }
  }

  public void onUpdateSelection(int paramInt1, int paramInt2, int paramInt3, int paramInt4, int paramInt5, int paramInt6)
  {
    super.onUpdateSelection(paramInt1, paramInt2, paramInt3, paramInt4, paramInt5, paramInt6);
    Singleton.getInstance().Debug("BlueInput::VIRTUALKB", "onUpdateSelection() called.");
  }

  public void swipeDown()
  {
    Singleton.getInstance().Debug("BlueInput::VIRTUALKB", "swipeDown() called.");
    handleClose();
  }

  public void swipeLeft()
  {
    Singleton.getInstance().Debug("BlueInput::VIRTUALKB", "swipeLeft() called.");
    handleBackspace();
  }

  public void swipeRight()
  {
    Singleton.getInstance().Debug("BlueInput::VIRTUALKB", "swipeRight() called.");
  }

  public void swipeUp()
  {
    Singleton.getInstance().Debug("BlueInput::VIRTUALKB", "swipeUp() called.");
  }

  void updateShiftKeyState(EditorInfo paramEditorInfo)
  {
    Singleton.getInstance().Debug("BlueInput::VIRTUALKB", "updateShiftKeyState() called.");
    EditorInfo localEditorInfo;
    if (paramEditorInfo != null)
    {
      localEditorInfo = getCurrentInputEditorInfo();
      if (localEditorInfo == null);
    }
  }
}